package com.maserb.monitor.remote;

import com.maserb.monitor.common.AjaxResult;
import com.maserb.monitor.common.AjaxUtils;
import com.maserb.monitor.common.Func_T;
import com.maserb.monitor.entity.card.AttendanceRecordEntity;
import com.maserb.monitor.entity.schoolStudent.SchoolStudentEntity;
import com.maserb.monitor.model.AttendanceResultModel;
import com.maserb.monitor.repository.schoolStudent.AttendanceRecordRepository;
import com.maserb.monitor.service.schoolStudent.SchoolStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 考勤接口
 */
@Controller
@RequestMapping("/attendance")
public class AttendanceController {

    @Autowired
    private SchoolStudentService schoolStudentService;
    @Autowired
    private AttendanceRecordRepository attendanceRecordRepository;

    /**
     * 考勤统计
     * @param clazzId
     * @param date 格式yyyyMMdd
     * @return
     */
        @RequestMapping("/attendanceStat")
    @ResponseBody
    public AjaxResult<AttendanceResultModel> attendanceStat(final String clazzId, final String date) {
        return AjaxUtils.process(new Func_T<AttendanceResultModel>() {
            @Override
            public AttendanceResultModel invoke() throws Exception {
                Integer onDuty = attendanceRecordRepository.countByClazzAndDate(clazzId, date+"%");
                Integer allCount = schoolStudentService.countByClass(clazzId);

                AttendanceResultModel stat = new AttendanceResultModel();
                stat.setAttendance(onDuty);
                stat.setTotal(allCount);

                return stat;
            }
        });
    }

    /**
     * 班级考勤详情
     * @param clazzId
     * @param date 格式yyyyMMdd
     * @Param type 1：已考勤 0：未考勤  不传则返回两个列表
     * @return
     */
    @RequestMapping("/attendanceStatDetail")
    @ResponseBody
    public AjaxResult<AttendanceResultModel> attendanceStatDetail(final String clazzId, final String date, String type) {
        return AjaxUtils.process(new Func_T<AttendanceResultModel>() {
            @Override
            public AttendanceResultModel invoke() throws Exception {
                List<SchoolStudentEntity> attendanceList = attendanceRecordRepository.findStudentByClazzAndDate(clazzId, date+"%");
                List<SchoolStudentEntity> allStudentList = schoolStudentService.findByClazzId(clazzId);

                AttendanceResultModel detail = new AttendanceResultModel();
                detail.setAttendanceList(attendanceList);
                allStudentList.removeAll(attendanceList);
                detail.setAbsenceList(allStudentList);

                return detail;
            }
        });
    }

    /**
     * 学生当日考勤记录
     * @param studentId
     * @param date 格式yyyyMMdd
     * @return
     */
    @RequestMapping("/attendanceList")
    @ResponseBody
    public AjaxResult<List<AttendanceRecordEntity>> attendanceList(String studentId, String date) {
        return AjaxUtils.process(new Func_T<List<AttendanceRecordEntity>>() {
            @Override
            public List<AttendanceRecordEntity> invoke() throws Exception {
                if(null==date || "".equals(date)){
                    return  attendanceRecordRepository.findByStudentId(studentId);
                }
                return attendanceRecordRepository.findByStudentIdAndDate(studentId, date+"%");
            }
        });
    }

}
